package org.parademo.equations.implementations;

import org.parademo.equations.base.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Random;

public class TestEquation_Stagger implements TestEquationInterface {

    public static final double e = 2.71828182845904523536;
    /**
     * To set the number of voters.
     */
    public int TOTAL_VOTERS = 100;
    /**
     * Weighted votes for each voter, calculated and updated.
     */
    ArrayList<Double> weightedAverages = new ArrayList<Double>(TOTAL_VOTERS);
    /**
     * Individual randomly generated voting timings and tokens.
     */
    int[] timings = new int[TOTAL_VOTERS];
    int[] tokens = {-4, -1, -4, -5, 4, 0, 5, 5, 0, 3, -5, 1, 0, 1, 4, -4, 1, -3, 1, 0, -5, 1, 0, 0, -1, -5, -5, -2, 1, -3, -5, 3, 0, 0, 0, -1, 5, 3, 3, 0, -2, 1, 1, 0, -4, -5, -2, 0, 1, -1, 3, 5, -5, -4, -2, 0, -5, 1, -2, 4, -3, 5, -4, 3, -1, -5, 0, -1, -1, -3, -2, 0, -1, -5, 1, -1, -2, 2, 0, 1, -2, 2, 3, -5, -4, 4, -4, -4, -4, 0, 5, -5, -3, 0, -2, -1, -3, 1, 0, -3};
    /**
     * Used for generating the tokens and list of timings.
     */
    Random randomGenerator = new Random();
    /**
     * Weighted totals for and against the proposal.
     */
    double Wa = 0.0;
    double Wf = 0.0;
    /**
     * Number of votes.
     */
    int plusVotes = 43;
    int negativeVotes = 41;
    /**
     * Total of tokens.
     */
    int plusTotal = 125;
    int negativeTotal = -119;
    /**
     * Temporary variables.
     */
    int x = 0, y = 0;

    /**
     * Implementation of the function.
     */
    public double calculate(double index) {

        double result = (-1 * Math.pow(index, 2.0)) / (1900 - Math.pow(index, 2.0));
        return Math.pow(e, result);
    }

    public void updateWeightedAverage() {

        double temp;

        for (int i = 0; i < TOTAL_VOTERS; i++) {

            x = this.tokens[i];

            if (i < 30) {
                y = randomGenerator.nextInt(30);
                System.out.println("Stagger1"+ "\t" + i + "\t" + y + "\n");

            } else if (i < 60 && i > 30) {
                y = randomGenerator.nextInt(20);
                System.out.println("Stagger2"+ "\t" + i + "\t" + y + "\n");

            } else if (i < TOTAL_VOTERS && i > 60) {
                y = randomGenerator.nextInt(10);
                System.out.println("Stagger3"+ "\t" + i + "\t" + y + "\n");

            }
            //tokens[i] = x;
            this.timings[i] = y;

            if (x > 0) {

                //plusVotes++;
                //plusTotal += x;

                temp = x * calculate(y);
                this.weightedAverages.add(temp);

                Wf += temp;

            } else if (x < 0) {

                //negativeVotes++;
                //negativeTotal += x;

                temp = x * calculate(y);
                this.weightedAverages.add(temp);

                Wa += temp;

            } else if (x == 0) {
                this.weightedAverages.add(0.0);
            }

        }

    }

    public static void main(String[] args) {

        Writer output;
        File file = new File("output_stagger.txt");

        try {
            output = new BufferedWriter(new FileWriter(file));
            String t1;

            output.write("Output of TestEquation_Stagger \n");
            output.write("------------------------------\n\n");
            output.write("Test cycle of 100 iterations with the votes and tokens as follows: \n\n");
            //output.write ("The Tokens array is : -4,-1,-4,-5,4,0,5,5,0,3,-5,1,0,1,4,-4,1,-3,1,0,-5,1,0,0,-1,-5,-5,-2,1,-3,-5,3,0,0,0,-1,5,3,3,0\n");
            //output.write ("-2,1,1,0,-4,-5,-2,0,1,-1,3,5,-5,-4,-2,0,-5,1,-2,4,-3,5,-4,3,-1,-5,0,-1,-1,-3,-2,0,-1,-5,1,-1,-2,2,0,1,-2,2,3,-5,-4,4,-4,-4,-4,0,5,-5,-3,0,-2,-1,-3,1,0,-3\n");
            output.write("The total votes for : 42 \n");
            output.write("The total votes against : 45 \n");

            output.write("The total tokens for : 130 \n");
            output.write("The total tokens against : -145 \n");

            for (int z = 0; z < 100; z++) {

                TestEquation_Stagger test = new TestEquation_Stagger();
                test.updateWeightedAverage();
                t1 = "";

                for (int ind : test.timings) {

                    t1 += (test.timings[ind] + ",");
                }

                output.write("************************************************************ \n");
                output.write("The timings array : " + t1 + "\n");
                output.write("The weighted total for : " + test.Wf + "\n");
                output.write("The weighted total against : " + test.Wa + "\n");
                output.write("************************************************************  \n");
            }
            output.close();

        } catch (IOException e) {

            e.printStackTrace();
        }

    }
}
